package com.dc.bobaoge.project.v2.to.v3.business.core.method.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dc.bobaoge.base.entity.core.po.ContactUser;
import com.dc.bobaoge.base.mapper.core.ContactUserMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreUserMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.business.system.account.common.em.AccountType;
import com.own.business.system.account.entity.SystemAccount;
import com.own.business.system.account.mapper.SystemAccountMapper;
import com.own.business.system.user.entity.po.SystemUserInfo;
import com.own.business.system.user.mapper.SystemUserInfoMapper;
import com.own.business.system.user.mapper.SystemUserMapper;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * UserUpdatePhoneResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-16 14:26:00
 */
@Slf4j
@Component
public class UserUpdatePhoneResolveMethod implements ResolveMethod {

    @Resource
    private OldCoreUserMapper oldCoreUserMapper;

    @Resource
    private SystemUserMapper systemUserMapper;
    @Resource
    private SystemAccountMapper systemAccountMapper;
    @Resource
    private SystemUserInfoMapper systemUserInfoMapper;
    @Resource
    private ContactUserMapper contactUserMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        // 查询用户信息是否存在
        var oldCoreUser = oldCoreUserMapper.selectById(trigger.getTargetId());
        if (oldCoreUser == null) {
            throw new BusinessSimpleException("用户信息不存在");
        }
        // 查询是否已经存在当前用户了
        var systemUser = systemUserMapper.selectById(oldCoreUser.getUserId());
        if (systemUser == null) {
            throw new BusinessSimpleException("用户信息不存在");
        }
        var systemUserInfo = getByUserId(systemUser.getId(), oldCoreUser.getUserPhone());
        // 获取用户的原手机号
        var oldPhone = systemUserInfo.getPhone();
        // 修改用户的账号信息
        {
            var updateWrapper1 = new LambdaUpdateWrapper<SystemAccount>()
                    .eq(SystemAccount::getFkUserId, systemUser.getId())
                    .eq(SystemAccount::getType, AccountType.PHONE.type())
                    .eq(SystemAccount::getAccount, oldPhone)
                    .set(SystemAccount::getAccount, oldCoreUser.getUserPhone());
            systemAccountMapper.update(updateWrapper1);
            var updateWrapper2 = new LambdaUpdateWrapper<SystemAccount>()
                    .eq(SystemAccount::getFkUserId, systemUser.getId())
                    .eq(SystemAccount::getType, AccountType.TEXT.type())
                    .eq(SystemAccount::getAccount, oldPhone)
                    .set(SystemAccount::getAccount, oldCoreUser.getUserPhone());
            systemAccountMapper.update(updateWrapper2);
        }
        // 修改用户的联系人信息
        {
            var updateWrapper1 = new LambdaUpdateWrapper<ContactUser>()
                    .eq(ContactUser::getFkUserId, systemUser.getId())
                    .eq(ContactUser::getPhone, oldPhone)
                    .set(ContactUser::getPhone, oldCoreUser.getUserPhone());
            contactUserMapper.update(updateWrapper1);

        }
        // 修改用户的详细信息
        if (!Objects.equals(oldPhone, oldCoreUser.getUserPhone())) {
            systemUserInfo.setPhone(oldCoreUser.getUserPhone());
            systemUserInfoMapper.updateById(systemUserInfo);
        }
    }

    /**
     * 获取用户的详细信息
     *
     * @param userId 用户id
     * @param phone  手机号
     * @return 用户信息
     */
    private SystemUserInfo getByUserId(Long userId, String phone) {
        var wrapper = new LambdaQueryWrapper<SystemUserInfo>().eq(SystemUserInfo::getFkUserId, userId);
        var userInfo = systemUserInfoMapper.selectOne(wrapper);
        if (userInfo == null) {
            userInfo = new SystemUserInfo();
            userInfo.setPhone(phone);
            userInfo.setFkUserId(userId);
            systemUserInfoMapper.insert(userInfo);
        }
        return userInfo;
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "user_phone";
    }

}
